home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 June: Reference Library / Dev.CD Jun 99 RL Disk 1.toast / Technical Documentation / Develop / develop Issue 28 / develop Issue 28 code / MacApp Debugging / TwistDownLists / UTwistDownElement.cp < prev    next >
Encoding:
Text File  |  1996-07-15  |  17.7 KB  |  514 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UTwistDownElement.cp
  3. // ETO20 MacApp 3.3.1, MPW 3.4.1
  4. // Copyright ©1995-1996 Conrad Kopala
  5. // Twist Down Lists version 2.0a0 7/15/96
  6. //----------------------------------------------------------------------------------------
  7.  
  8. #ifndef __UTWISTDOWNELEMENT__
  9. #include "UTwistDownElement.h"
  10. #endif
  11.  
  12. #ifndef __UTWISTDOWNAPP__
  13. #include "UTwistDownApp.h"
  14. #endif
  15.  
  16. #ifndef __UTWISTDOWNDOCUMENT__
  17. #include "UTwistDownDocument.h"
  18. #endif
  19. /*
  20. #ifndef __UTWISTDOWNVIEW__
  21. #include "UTwistDownView.h"
  22. #endif
  23. */
  24. #ifndef __UTWISTDOWNGLOBALS__
  25. #include "UTwistDownGlobals.h"
  26. #endif
  27.  
  28. //MacApp stuff
  29. #ifndef __MACAPPTYPES__
  30. #include "MacAppTypes.h"
  31. #endif
  32.  
  33. #ifndef __UDEBUG__
  34. #include "UDebug.h"        //PPC
  35. #endif
  36.  
  37. //ToolBox stuff
  38. #ifndef __AEREGISTRY__
  39. #include "AERegistry.h"
  40. #endif
  41.  
  42. //ANSI stuff
  43. //None
  44. //========================================================================================
  45. // CLASS TTwistDownElement
  46. //========================================================================================
  47. #undef Inherited
  48. #define Inherited TObject
  49.  
  50. #pragma segment AOpen
  51. MA_DEFINE_CLASS_M2(TTwistDownElement, Inherited, MScriptableObject);
  52.  
  53. //----------------------------------------------------------------------------------------
  54. // TTwistDownElement::TTwistDownElement
  55. //----------------------------------------------------------------------------------------
  56. #pragma segment AOpen
  57. TTwistDownElement::TTwistDownElement(): MScriptableObject(cTwistDownElement)
  58. {
  59. fTwistDownDocument = NULL;
  60. fSuccessorElement = NULL;
  61. fDescendantElement = NULL;
  62. fItemInList = 0;
  63. fIndentLevel = 0;
  64. fHasSubList = FALSE;
  65. fShowSubList = FALSE;
  66. fOldShowSubList = FALSE;
  67. fDisplayedText = gEmptyString;
  68. fTwistDownControl = NULL;
  69.  
  70. #if qDebug
  71. this -> PrintAppConstructorClassInfo();
  72. #endif
  73.  
  74. }
  75. //----------------------------------------------------------------------------------------
  76. // TTwistDownElement::~TTwistDownElement
  77. //----------------------------------------------------------------------------------------
  78. #pragma segment AClose
  79. TTwistDownElement::~TTwistDownElement()
  80. {
  81. fTwistDownDocument = NULL;
  82. fSuccessorElement = NULL;
  83. fDescendantElement = NULL;
  84. fDisplayedText = gEmptyString;
  85.  
  86. #if qDebug
  87. this -> PrintAppDestructorClassInfo();
  88. #endif
  89.  
  90. }
  91. //----------------------------------------------------------------------------------------
  92. // TTwistDownElement::ITwistDownElement
  93. //----------------------------------------------------------------------------------------
  94. #pragma segment AReadFile
  95. void TTwistDownElement::ITwistDownElement(TTwistDownDocument* twistDownDocument, short indentLevel, 
  96.                                                                                     CStr63 displayedText)
  97. {
  98. this -> IObject();
  99. fTwistDownDocument = twistDownDocument;
  100. fIndentLevel = indentLevel;
  101. fHasSubList = FALSE;
  102. fShowSubList = FALSE;
  103. fOldShowSubList = FALSE;
  104. fDisplayedText = displayedText;
  105. }
  106. //----------------------------------------------------------------------------------------
  107. // TTwistDownElement::GetDisplayedText
  108. //----------------------------------------------------------------------------------------
  109. #pragma segment ARes
  110. void TTwistDownElement::GetDisplayedText(CStr63& theDisplayedText)
  111. {
  112. theDisplayedText = fDisplayedText;
  113. }
  114. //----------------------------------------------------------------------------------------
  115. // TTwistDownElement::SetHasSubListFlag    //is being used    -s
  116. //----------------------------------------------------------------------------------------
  117. #pragma segment AReadFile
  118. void TTwistDownElement::SetHasSubListFlag()
  119. {
  120. fHasSubList = TRUE;
  121. }
  122. //----------------------------------------------------------------------------------------
  123. // TTwistDownElement::ClearHasSubListFlag    Is never used. Included for completeness.
  124. //----------------------------------------------------------------------------------------
  125. #pragma segment ARes
  126. void TTwistDownElement::ClearHasSubListFlag()
  127. {
  128. fHasSubList = FALSE;
  129. }
  130. //----------------------------------------------------------------------------------------
  131. // TTwistDownElement::TestHasSubListFlag    //is being used
  132. //----------------------------------------------------------------------------------------
  133. #pragma segment ARes
  134. Boolean TTwistDownElement::TestHasSubListFlag()
  135. {
  136. return fHasSubList;
  137. }
  138. //----------------------------------------------------------------------------------------
  139. // TTwistDownElement::SetShowSubListFlag    used by TTwistDownDocument::SetShowSubListFlags
  140. //----------------------------------------------------------------------------------------
  141. //Good programming practice: The calling methods should make sure it is appropriate to
  142. //call SetShowSubListFlag() before calling it. To enforce that practice, I've put in the
  143. //ProgramBreak.
  144.  
  145. #pragma segment ARes
  146. void TTwistDownElement::SetShowSubListFlag()
  147. {
  148. if (this -> TestHasSubListFlag())    //Make sure it makes sense to set this flag. Defensive programming.
  149.     {
  150.         fOldShowSubList = fShowSubList;
  151.         fShowSubList = TRUE;
  152.     }
  153. #if qDebug
  154. else
  155.         ProgramBreak("Attempted to set fShowSubList when fHasSubList == FALSE!");
  156. #endif
  157. }
  158. //----------------------------------------------------------------------------------------
  159. // TTwistDownElement::ClearShowSubListFlag    Used by TTwistDownDocument::ClearShowSubListFlags
  160. //----------------------------------------------------------------------------------------
  161. //Good programming practice: The calling methods should make sure it is appropriate to
  162. //call ClearShowSubListFlag() before calling it. To enforce that practice, I've put in the
  163. //ProgramBreak.
  164.  
  165. #pragma segment ARes
  166. void TTwistDownElement::ClearShowSubListFlag()
  167. {
  168. if (this -> TestHasSubListFlag())    //Make sure it makes sense to clear this flag. //Defensive programming.
  169.     {
  170.         fOldShowSubList = fShowSubList;    //maybe I should just set to FALSE??
  171.         fShowSubList = FALSE;                        
  172.     }
  173.     
  174. #if qDebug
  175. else if (this -> TestHasSubListFlag() == FALSE)
  176.     ProgramBreak("Attempted to clear fShowSubList when fHasSubList == FALSE!");
  177. else
  178.     ProgramBreak("ClearShowSubListFlag- fHasSublist == TRUE");
  179. #endif
  180.  
  181. }
  182. //----------------------------------------------------------------------------------------
  183. // TTwistDownElement::RestoreShowSubListFlag    used by TTwistDownDocument::RestoreShowSubListFlags
  184. //----------------------------------------------------------------------------------------
  185. //Good programming practice: The calling methods should make sure it is appropriate to
  186. //call ClearShowSubListFlag() before calling it. To enforce that practice, I've put in the
  187. //ProgramBreak.
  188.  
  189. #pragma segment ARes
  190. void TTwistDownElement::RestoreShowSubListFlag()
  191. {
  192. if (this -> TestHasSubListFlag())    //Make sure it makes sense to clear this flag. //Defensive programming.
  193.     {
  194.         fShowSubList = fOldShowSubList;
  195.         fOldShowSubList = FALSE;    
  196.     }
  197.     
  198. #if qDebug
  199. else if (this -> TestHasSubListFlag() == FALSE)
  200.     ProgramBreak("Attempted to clear fShowSubList when fHasSubList == FALSE!");
  201. else
  202.     ProgramBreak("ClearShowSubListFlag- fHasSublist == TRUE");
  203. #endif
  204.  
  205. }
  206. //----------------------------------------------------------------------------------------
  207. // TTwistDownElement::TestShowSubListFlag    //is being used
  208. //----------------------------------------------------------------------------------------
  209. #pragma segment ARes
  210. Boolean TTwistDownElement::TestShowSubListFlag()
  211. {
  212. return fShowSubList;
  213. }
  214. //----------------------------------------------------------------------------------------
  215. // TTwistDownElement::SetOldShowSubListFlag    //is being used
  216. //----------------------------------------------------------------------------------------
  217. //Good programming practice: The calling methods should make sure it is appropriate to
  218. //call SetShowSubListFlag() before calling it. To enforce that practice, I've put in the
  219. //ProgramBreak.
  220.  
  221. #pragma segment ARes
  222. void TTwistDownElement::SetOldShowSubListFlag()
  223. {
  224. if (this -> TestHasSubListFlag())    //Make sure it makes sense to set this flag. Defensive programming.
  225.     {
  226.         fOldShowSubList = TRUE;
  227.     }
  228. #if qDebug
  229. else
  230.         ProgramBreak("Attempted to set fOldShowSubList when fHasSubList == FALSE!");
  231. #endif
  232. }
  233. //----------------------------------------------------------------------------------------
  234. // TTwistDownElement::ClearOldShowSubListFlag    //is being used
  235. //----------------------------------------------------------------------------------------
  236. //Good programming practice: The calling methods should make sure it is appropriate to
  237. //call ClearShowSubListFlag() before calling it. To enforce that practice, I've put in the
  238. //ProgramBreak.
  239.  
  240. #pragma segment ARes
  241. void TTwistDownElement::ClearOldShowSubListFlag()
  242. {
  243. if (this -> TestHasSubListFlag())    //Make sure it makes sense to clear this flag. //Defensive programming.
  244.     {
  245.         fOldShowSubList = FALSE;        
  246.     }
  247.     
  248. #if qDebug
  249. else if (this -> TestHasSubListFlag() == FALSE)
  250.     ProgramBreak("Attempted to clear fOldShowSubList when fHasSubList == FALSE!");
  251. else
  252.     ProgramBreak("ClearOldShowSubListFlag- fHasSublist == TRUE");
  253. #endif
  254.  
  255. }
  256. //----------------------------------------------------------------------------------------
  257. // TTwistDownElement::TestOldShowSubListFlag    //is being used
  258. //----------------------------------------------------------------------------------------
  259. #pragma segment ARes
  260. Boolean TTwistDownElement::TestOldShowSubListFlag()
  261. {
  262. return fOldShowSubList;
  263. }
  264. //----------------------------------------------------------------------------------------
  265. // TTwistDownElement::SetSuccessorElementTo
  266. //----------------------------------------------------------------------------------------
  267. #pragma segment AReadFile
  268. void TTwistDownElement::SetSuccessorElementTo(TTwistDownElement* theSuccessorElement)
  269. {
  270. fSuccessorElement = theSuccessorElement;
  271. }
  272. //----------------------------------------------------------------------------------------
  273. // TTwistDownElement::SetDescendantElementTo
  274. //----------------------------------------------------------------------------------------
  275. #pragma segment AReadFile
  276. void TTwistDownElement::SetDescendantElementTo(TTwistDownElement* theDescendantElement)
  277. {
  278. fDescendantElement = theDescendantElement;
  279. }
  280. //----------------------------------------------------------------------------------------
  281. // TTwistDownElement::GetSuccessorElement
  282. //----------------------------------------------------------------------------------------
  283. #pragma segment ARes
  284. TTwistDownElement*    TTwistDownElement::GetSuccessorElement()
  285. {
  286. return fSuccessorElement;
  287. }
  288. //----------------------------------------------------------------------------------------
  289. // TTwistDownElement::GetDescendantElement
  290. //----------------------------------------------------------------------------------------
  291. #pragma segment ARes
  292. TTwistDownElement*    TTwistDownElement::GetDescendantElement()
  293. {
  294. return fDescendantElement;
  295. }
  296. //----------------------------------------------------------------------------------------
  297. // TTwistDownElement::SetTwistDownControlTo
  298. //----------------------------------------------------------------------------------------
  299. #pragma segment ARes
  300. void TTwistDownElement::SetTwistDownControlTo(TTwistDownControl* theTwistDownControl)
  301. {
  302. fTwistDownControl = theTwistDownControl;
  303. }
  304. //----------------------------------------------------------------------------------------
  305. // TTwistDownElement::GetTwistDownControl
  306. //----------------------------------------------------------------------------------------
  307. #pragma segment ARes
  308. TTwistDownControl*    TTwistDownElement::GetTwistDownControl()
  309. {
  310. return fTwistDownControl;
  311. }
  312. //----------------------------------------------------------------------------------------
  313. // TTwistDownElement::SetIndentLevel is not being used, included for completeness.
  314. //----------------------------------------------------------------------------------------
  315. #pragma segment AReadFile
  316. void TTwistDownElement::SetIndentLevel(short indentLevel)
  317. {
  318. fIndentLevel = indentLevel;
  319. }
  320. //----------------------------------------------------------------------------------------
  321. // TTwistDownElement::GetIndentLevel    //is being used
  322. //----------------------------------------------------------------------------------------
  323. #pragma segment ARes
  324. short TTwistDownElement::GetIndentLevel()
  325. {
  326. return fIndentLevel;
  327. }
  328. //----------------------------------------------------------------------------------------
  329. // TTwistDownElement::SetItemInList    -s
  330. //----------------------------------------------------------------------------------------
  331. #pragma segment AReadFile
  332. void TTwistDownElement::SetItemInList(long itemInList)
  333. {
  334. fItemInList = itemInList;
  335. }
  336. //----------------------------------------------------------------------------------------
  337. // TTwistDownElement::GetItemInList
  338. //----------------------------------------------------------------------------------------
  339. #pragma segment ARes
  340. long TTwistDownElement::GetItemInList()
  341. {
  342. return fItemInList;
  343. }
  344. //----------------------------------------------------------------------------------------
  345. // TTwistDownElement::CouldExpandElement
  346. //Tests to make sure the capacity of TTwistDownView (TTextListView) will not be exceeded by an 
  347. //attempt to expand the element.
  348. //----------------------------------------------------------------------------------------
  349. #pragma segment ARes
  350. Boolean TTwistDownElement::CouldExpandElement()
  351. {
  352. Boolean result = FALSE;
  353. long newNumberOfRows = 0;
  354.  
  355. this -> SetShowSubListFlag();
  356. newNumberOfRows  = fTwistDownDocument -> CountVisibleElements(fTwistDownDocument -> GetHeadOfList());
  357.  
  358. if (newNumberOfRows  <= (long)kMaxNumberOfItemsToDisplay)
  359.             result = TRUE;
  360.  
  361. this -> ClearShowSubListFlag();
  362.  
  363. return result;
  364. }
  365. //----------------------------------------------------------------------------------------
  366. // TTwistDownElement::DoScriptCommand:     Scripting Support
  367. //----------------------------------------------------------------------------------------
  368. #pragma segment MAScriptingRes
  369. void TTwistDownElement::DoScriptCommand(CommandNumber    aCommandNumber, TAppleEvent* message, TAppleEvent* reply)
  370. {
  371.     switch (aCommandNumber)
  372.     {
  373.         case cExpandElement:
  374.             {    
  375.                 if (this -> TestHasSubListFlag())    //Make sure it is a folder.
  376.                     {
  377.                         if (!this -> TestShowSubListFlag())    //Make sure it isn't already expanded.
  378.                             {
  379.                                 if (this -> CouldExpandElement())    //Make sure we won't exceed max number of rows.
  380.                                     {
  381.                                         TExpandElementCommand* anExpandElementCommand = new TExpandElementCommand;
  382.                                         anExpandElementCommand -> IExpandElementCommand(fTwistDownDocument, this, 
  383.                                                                                                                                         message -> fMessage, reply -> fMessage);
  384.                                         fTwistDownDocument -> PostCommand(anExpandElementCommand);
  385.                                     }
  386.                                 else
  387.                                     FailOSErr(errCantExpandElement);
  388.                             }
  389.                         else
  390.                             FailOSErr(errElementAlreadyExpanded);
  391.                     }
  392.                 else
  393.                     FailOSErr(errAENotAFolder);
  394.             }
  395.             break;
  396.  
  397.         case cCollapseElement:
  398.             {    
  399.                 if (this -> TestHasSubListFlag())    //Make sure it is a folder.
  400.                     {
  401.                         if (this -> TestShowSubListFlag())    //Make sure it isn't already collpsed.
  402.                             {
  403.                             //Because we don't want this to fail for out of memory reasons,
  404.                             //allocate it from temporary memory.    //PGMacApp 550
  405.                             //Note that TCollapseElementCommand may recover memory used by
  406.                             //TTwistDownControls.
  407.                                 Boolean oldTemp = TemporaryAllocation(TRUE);
  408.                                 Boolean oldArrayPerm = AllocateObjectsFromPerm(FALSE);
  409.                                     TCollapseElementCommand* aCollapseElementCommand = new TCollapseElementCommand;
  410.                                     aCollapseElementCommand -> ICollapseElementCommand(fTwistDownDocument, this, 
  411.                                                                                                                                         message -> fMessage, reply -> fMessage);
  412.                                     
  413.                                     fTwistDownDocument -> PostCommand(aCollapseElementCommand);
  414.                                 oldArrayPerm = AllocateObjectsFromPerm(oldArrayPerm);
  415.                                 oldTemp = TemporaryAllocation(oldTemp);
  416.                             }
  417.                         else
  418.                             FailOSErr(errElementAlreadyCollapsed);    
  419.  
  420.                     }
  421.                 else
  422.                     FailOSErr(errAENotAFolder);                    
  423.             }
  424.             break;
  425.             
  426.         default:
  427.             {
  428.                 MScriptableObject::DoScriptCommand(aCommandNumber, message, reply);
  429.             }
  430.             break;
  431.     }
  432. }
  433. //----------------------------------------------------------------------------------------
  434. //  TTwistDownElement::GetSpecifierForm:        Scripting Support
  435. //----------------------------------------------------------------------------------------
  436. #pragma segment MAScriptingRes
  437. DescType  TTwistDownElement::GetSpecifierForm()
  438. {
  439.     return formName;
  440. }
  441. //----------------------------------------------------------------------------------------
  442. //  TTwistDownElement::GetObjectProperty:        Scripting Support
  443. //----------------------------------------------------------------------------------------
  444. #pragma segment MAScriptingRes
  445. Boolean  TTwistDownElement::GetObjectProperty(CAEDesc& thePropertyValue,
  446.                                         DescType whichProperty,
  447.                                         const CAEDesc& desiredType)
  448. {
  449.  
  450.     Boolean hasProperty = TRUE;
  451.     
  452.     FailInfo fi;
  453.     Try(fi)
  454.     {
  455.         switch (whichProperty)
  456.         {
  457.     
  458.             case pName:
  459.                 CStr255 theName;
  460.                 theName = fDisplayedText;            
  461.                 thePropertyValue.PutString(theName);
  462.                 break;
  463.  
  464.             case pHasSubList:
  465.                 thePropertyValue.PutBoolean(fHasSubList);
  466.                 break;
  467.                 
  468.             case pShowSubList:
  469.                 thePropertyValue.PutBoolean(fShowSubList);
  470.                 break;
  471.                 
  472.             case pHasControl:
  473.                 {
  474.                     Boolean hasControl = FALSE;
  475.                     if (fTwistDownControl)
  476.                         hasControl = TRUE;
  477.                     thePropertyValue.PutBoolean(hasControl);
  478.                 }
  479.                 break;
  480.                 
  481.             case pItemInList:
  482.                 thePropertyValue.PutLong(fItemInList);
  483.                 break;    
  484.  
  485.             case pIndentLevel:
  486.                 long itsIndentLevel = (long)fIndentLevel;    
  487.                 thePropertyValue.PutLong(itsIndentLevel);
  488.                 break;
  489.                 
  490.             default:
  491.                 hasProperty = MScriptableObject::GetObjectProperty(thePropertyValue, whichProperty, desiredType);
  492.                 break;
  493.         }
  494.         fi.Success();
  495.     }
  496.     else
  497.     {
  498.         // we failed while trying to get the property
  499.         hasProperty = FALSE;
  500.         
  501.         // Don't ReSignal
  502.     }
  503.     return hasProperty;
  504. }
  505. //----------------------------------------------------------------------------------------
  506. // TTwistDownElement::GetObjectsContainer
  507. //----------------------------------------------------------------------------------------
  508. #pragma segment MAOSLDispatch
  509. MScriptableObject* TTwistDownElement::GetObjectsContainer()
  510. {
  511.     return (MScriptableObject*)fTwistDownDocument;
  512. }
  513.  
  514. #pragma segment Inline